//
//  AreaGetter.m
//  Hotel
//
//  Created by 周国勇 on 2/26/14.
//  Copyright (c) 2014 jokefaker. All rights reserved.
//

#import "AreaGetter.h"

@implementation AreaGetter
/// 获得省份
+ (NSArray *)provinces
{
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:@"area" ofType:@"plist"];
	NSDictionary *areaDic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    NSArray *components = [areaDic allKeys];
    NSArray *sortedArray = [components sortedArrayUsingComparator: ^(id obj1, id obj2) {
        
        if ([obj1 integerValue] > [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        
        if ([obj1 integerValue] < [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    }];
    NSMutableArray *provinceTmp = [[NSMutableArray alloc] init];
    for (int i=0; i<[sortedArray count]; i++) {
        NSString *index = [sortedArray objectAtIndex:i];
        NSArray *tmp = [[areaDic objectForKey: index] allKeys];
        [provinceTmp addObject: [tmp objectAtIndex:0]];
    }
    
    return provinceTmp;
    
}
/// 获得省份下的城市
+ (NSArray *)citiesForProvince:(NSInteger)provinceIndex
{
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:@"area" ofType:@"plist"];
	NSDictionary *areaDic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

    
    NSDictionary *tmp = [NSDictionary dictionaryWithDictionary: [areaDic objectForKey: [NSString stringWithFormat:@"%d", provinceIndex]]];
    NSArray *allKeys = [tmp allKeys];
    NSString *provinceName = allKeys[0];
    NSDictionary *cityDic = [tmp objectForKey:provinceName];
    NSArray *cityIndexs = [cityDic allKeys];
    
    NSArray *sortedIndexArray = [cityIndexs sortedArrayUsingComparator: ^(id obj1, id obj2) {
        
        if ([obj1 integerValue] > [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedDescending;//递减
        }
        
        if ([obj1 integerValue] < [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedAscending;//上升
        }
        return (NSComparisonResult)NSOrderedSame;
    }];
    
    NSMutableArray *array = [[NSMutableArray alloc] init];
    for (int i=0; i<[sortedIndexArray count]; i++) {
        NSString *index = [sortedIndexArray objectAtIndex:i];
        NSArray *temp = [[cityDic objectForKey: index] allKeys];
        [array addObject: [temp objectAtIndex:0]];
    }
    return array;
    
}
/// 获得城市下的区域
+ (NSArray *)districtForCity:(NSInteger)cityIndex andProvince:(NSInteger)provinceIndex
{
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:@"area" ofType:@"plist"];
	NSDictionary *areaDic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    
    
    NSDictionary *tmp = [NSDictionary dictionaryWithDictionary: [areaDic objectForKey: [NSString stringWithFormat:@"%d", provinceIndex]]];
    NSArray *allKeys = [tmp allKeys];
    NSString *provinceName = allKeys[0];
    NSDictionary *cityDic = [tmp objectForKey:provinceName];
    NSArray *cityIndexs = [cityDic allKeys];
    
    NSArray *sortedIndexArray = [cityIndexs sortedArrayUsingComparator: ^(id obj1, id obj2) {
        
        if ([obj1 integerValue] > [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedDescending;//递减
        }
        
        if ([obj1 integerValue] < [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedAscending;//上升
        }
        return (NSComparisonResult)NSOrderedSame;
    }];
    NSString *cityIndexKey = [sortedIndexArray objectAtIndex:cityIndex];
    NSDictionary *districtDic = [cityDic objectForKey: cityIndexKey];
    NSArray *allCityKeys = [districtDic allKeys];
    NSString *disKey = [allCityKeys objectAtIndex:0];
    return [districtDic objectForKey:disKey];

}

@end
